﻿namespace MicroCloud.SMS
{
    #region "短信模块基类"
    /// <summary>
    /// 短信模块基类
    /// </summary>
    /// <typeparam name="TVerifyCode">验证码短信记录实体类型</typeparam>
    /// <typeparam name="TVerifyCodeKey">验证码短信记录实体主键类型</typeparam>
    /// <typeparam name="IVerifyCodeEventData">短信验证码发送事件数据类型</typeparam>
    /// <typeparam name="TVerifyCodeService">短信验证码服务类型</typeparam>
    [Description("短信模块")]
    [DependsOnPacks(typeof(EventBusPack), typeof(AspNetCorePack))]
    public abstract class SmsPackBase<TVerifyCode, TVerifyCodeKey, IVerifyCodeEventData, TVerifyCodeService> : AspPackBase
        where TVerifyCode : VerifyCodeBase<TVerifyCodeKey>, new()
        where TVerifyCodeKey : IEquatable<TVerifyCodeKey>
        where IVerifyCodeEventData : SmsVerifyCodeEventDataBase<TVerifyCode, TVerifyCodeKey>, new()
        where TVerifyCodeService : SmsVerifyCodeServiceBase<TVerifyCode, TVerifyCodeKey, IVerifyCodeEventData>, ISmsVerifyCodeService
    {
        #region "属性"
        /// <summary>
        /// 获取 模块级别
        /// <para>级别越小越先启动</para>
        /// </summary>
        public override PackLevel Level => PackLevel.Business;

        /// <summary>
        /// 获取 模块启动顺序编号
        /// <para>模块启动的顺序先按级别启动，级别内部再按此顺序启动，级别默认为0，表示无依赖，需要在同级别有依赖顺序的时候，再重写为 >0 的顺序值。</para>
        /// </summary>
        public override int OrderNo => 0;

        #endregion

        #region "方法"
        #region "将模块服务添加到依赖注入服务容器中"
        /// <summary>
        /// 将模块服务添加到依赖注入服务容器中
        /// </summary>
        /// <param name="services">依赖注入服务容器</param>
        /// <returns>服务集合</returns>
        public override IServiceCollection AddServices(IServiceCollection services)
        {
            SmsOptions smsOptions = services.GetConfiguration()?.GetSmsOptions();
            switch (smsOptions.ProviderEnum)
            {
                case SmsProvider.Aliyun:
                    services.TryAddSingleton<ISmsInquirer, AliyunSmsInquirer>();
                    services.AddSingleton<ISmsSender, AliyunSmsSender>();
                    services.TryAddSingleton<ISmsSigner, AliyunSmsSigner>();
                    services.TryAddSingleton<ISmsTemplater, AliyunSmsTemplater>();
                    break;
                case SmsProvider.YFXYT:
                    services.TryAddSingleton<ISmsInquirer, YfxytSmsInquirer>();
                    services.AddSingleton<ISmsSender, YfxytSmsSender>();
                    services.TryAddSingleton<ISmsSigner, YfxytSmsSigner>();
                    services.TryAddSingleton<ISmsTemplater, YfxytSmsTemplater>();
                    break;
            }
            services.AddScoped<ISmsVerifyCodeService, TVerifyCodeService>();

            //注册托管服务
            services.AddHostedService<SmsHostedService<TVerifyCode, TVerifyCodeKey, IVerifyCodeEventData>>();
            //注册事件委托
            services.AddEventHandler<ConfigurationChangeEventHandler>();

            return services;
        }
        #endregion

        #endregion

    }
    #endregion

}
